// 加密压缩打包后的小组件代码
// 方便隐藏敏感信息，减少组件体积和保护小组件不被随意修改

// 用法：
// node encode.js Scripts/小组件.js

const process = require('process');
const os = require('os');
const fs = require('fs');
const path = require('path');

var JB = require('javascript-obfuscator');

if (process.argv.length !== 3) {
  console.log('[!] 用法：node encode.js Dist/xxx.js');
  process.exit(0);
}

const file_name = process.argv[2];
const out_name = file_name.replace('Scripts', 'Dist').replace('.js', '.min.js');

// 读取源文件
const widget_file = fs.readFileSync(path.join(__dirname, file_name));

let widget_code = widget_file.toString('utf-8');

var result = JB.obfuscate(widget_code.toString('utf-8'), {
  rotateStringArray: true,
  selfDefending: true,
  stringArray: true,
  splitStringsChunkLength: 100,
  stringArrayEncoding: ['rc4', 'base64'],
}).getObfuscatedCode();

fs.writeFileSync(path.join(__dirname, out_name), `\n${result}\n`);

console.log('[*] 文件已压缩混淆至：', out_name);
